package com.gupao.rpc.protocol.client;

import com.gupao.rpc.protocol.constants.ReqType;
import com.gupao.rpc.protocol.protocol.*;
import com.gupao.rpc.protocol.spring.SpringBeanManager;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import lombok.extern.slf4j.Slf4j;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * @author SuperWein
 */
@Slf4j
public class ClientChannelHandler extends SimpleChannelInboundHandler<RpcProtocol<RpcResponse>> {

    @Override
    protected void channelRead0(ChannelHandlerContext channelHandlerContext, RpcProtocol<RpcResponse> requestRpcProtocol) throws Exception {
        log.info("receive Rpc Server Result");
        long requestId = requestRpcProtocol.getHeader().getRequestId();
        RpcFuture<RpcResponse> future = RequestHolder.REQUEST_MAP.remove(requestId);
        // 返回结果
        future.getPromise().setSuccess(requestRpcProtocol.getBody());
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        // super.exceptionCaught(ctx, cause);
        log.error("------>>>>> receive Rpc Server Result exception ", cause);
        ctx.channel().closeFuture();
    }

}
